GraphQL API
DevAssure provides comprehensive support for GraphQL API Testing, enabling you to seamlessly integrate "GraphQL queries" and mutations into your automated testing workflows. This document outlines the features and capabilities of DevAssure's GraphQL support.
Adding a GraphQL Request
-
To add a GraphQL request, follow the general steps for creating an API request as described in the Create API Request documentation.
-
When configuring the request body:
- Enter Endpoint URL: Provide the URL of your GraphQL endpoint in the "Base URL" field.
- Select GraphQL: In the "Body" section of the request configuration, choose the "GraphQL" option.
- Input GraphQL Query: Enter your GraphQL query in the "GraphQL Query" editor. DevAssure separates the input for queries and variables, allowing for clear and organized test creation.
GraphQL Variables
DevAssure allows you to use variables within your GraphQL queries for dynamic requests:
-
Declare Variables: Define your variables within the "GraphQL Variables" editor. These are typically provided as a JSON object.
-
Reference Variables in Query: Reference the declared variables inside your GraphQL query using the
$
prefix.
Example: Fetching Product Details
Imagine you have a GraphQL API for an e-commerce platform. You want to test a query that retrieves product details by ID.
1. The GraphQL Query:
query GetProduct($productId: ID!) {
product(id: $productId) {
id
name
description
price
category {
name
}
reviews {
rating
comment
}
}
}
This query, GetProduct, takes a required productId as input (of type ID!) and returns details about the product, including its name, description, price, category, and reviews.
2. Setting up the Request
- Paste the above GetProduct query into the "GraphQL Query" editor.
- In the "GraphQL Variables" section, provide the productId value. For example, to fetch product with ID "P123", enter the following JSON:
{
"productId": "P123"
}
- Click the "Test" button to send the GraphQL request to your API.
3. Validating the Response
After executing the request, DevAssure will display the response.
You can now validate the results:
- Status Code: Verify that the response code is 200 (OK).
- Response Body: Examine the JSON response to ensure it contains the expected product data. For example, you could check if the name field matches the expected product name, the price is within a certain range, or if the category name is correct.
{
"data": {
"product": {
"id": "P123",
"name": "Awesome T-Shirt",
"description": "A comfortable and stylish t-shirt.",
"price": 29.99,
"category": {
"name": "Apparel"
},
"reviews": [
{
"rating": 5,
"comment": "Great shirt!"
},
{
"rating": 4,
"comment": "Very comfortable."
}
]
}
}
}
4. Integrating into a Test Case
This GraphQL query and its validation steps can be incorporated directly into a DevAssure test case using the API Test actions. This allows you to automate the product data retrieval and verification as part of a larger test flow, for example, after adding a product to the cart or completing a checkout process.